package com.example.demo.server3.handler;

import com.example.demo.server3.template.MessageHeader;
import com.example.demo.server3.template.ProtocolTemplate;
import com.example.demo.server3.tool.TemplateTool;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class DecodeHandler extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        MessageHeader messageHeader = new MessageHeader();
        ByteBuf byteBuf = Unpooled.buffer(12);
        in.getBytes(0,byteBuf);
        messageHeader.structure(byteBuf);
        ProtocolTemplate protocolTemplate = TemplateTool.parse(messageHeader,in.readBytes(messageHeader.getTotal_Length()));
        out.add(protocolTemplate);
    }
}
